FRC Java: Simple Robot
Introduction Simple robot is a basic robot program layout that has appeal to knew teams or teams starting out with java. This page will explain the parts of the basic parts of the simple robot project and how to use it to make basic functioning FRC code by explaining a piece of example code. The Robot Class File The robot class file is the main file of your project and is created when you create and named when you make your project. the simple robot default class file can be simplified into four major sections that have different attributes, will do different things and require different usage than each other. The Import section This section contains all of the import statements in your code and a package statement, any other statements placed in this section will create errors. Packages: The package statement tells your program that it can use code within the package for your FRC java project so you do not need to import files within that package for your program to have access to them. Imports: imports for your program, any code that is not in your project package will need to be imported. This includes things like motor controller programs written by first and java code for functions that are not included by default. The standard format for an import statement is: import ; Some basic inputs you are likely to need: * Joystick - The basic input used by most teams provides resources for easily getting the state of the joystick * JoystickButton - Simple system for reading joystick buttons as true/false values indicating whether or not they have been pushed * RobotDrive - A commonly used drive train control system that lets you set speed for each side of the robot * Jaguar/Victor/Talon - These are the three most common FIRST motor controllers, If you decide to control your robots drive motors without using a RobotDrive or want to control the speed of an additional motor this will do it. (for more information about using these controllers go here) * Servo - Allows you to control a servo and designate its position in degrees * Timer - Allows you to delay any code after it until the designated number of seconds have elapsed Import Errors If you are using Netbeans any time you write a program that uses another program that you have not imported it will automatically suggest that you import that program by placing a red dot and a light bulb next to the line you would need an input to run. The Class The class is the primary area of your code, it contains all of your variables, local functions and logical statements. The class contains both of the following two sections but it can also contain things outside of them. Things that can be placed outside of the autonomous and teleop sections include variables and objects you want to be available for both, most of the functions that you write will also go here. In the example the program at the bottom of this page in this section we declare and initialize * Two Joysticks, at usb ports 1 and 2 * A RobotDrive at pwm slots 1 and 2 Autonomous Section This section will contain pre-written commands for your robot that will run once during the autonomous period of a match. In FRC the autonomous period is a no control period and your drivers will not be able to use the controls so inputting controls for your robot is pointless unless it's something like a switch that you set before a match to designate a certain autonomous action or sequence. In the example: At the bottom of the page you can find an example of simple robot code including a simple autonomous where the robot's speed is set to half speed and then after one second it is set to zero resulting in one second of forward movement. Teleop Section This section is interchangeably referred to as operator control, teleop or the teleoperated section. This section runs once during the teleoperated period and should contain a loop that runs throughout all of teleop otherwise any actions it does will occur once. In the example: The primary way to control the robot is to systematically check the inputs and set the outputs. In the bellow program you see an example where the y (forward/backward) values of the joysticks leftStick and rightStick are retrieved using a get command and are directly being used to control the wheel movement for the left and right sides of the robot. Basic Simple Robot Code //Import Section package edu.wpi.first.wpilibj.templates; import edu.wpi.first.wpilibj.SimpleRobot; import edu.wpi.first.wpilibj.Joystick; import edu.wpi.first.wpilibj.RobotDrive; import edu.wpi.first.wpilibj.Timer; //Robot Class Section public class RobotTemplate extends SimpleRobot { Joystick leftStick = new Joystick(1); Joystick rightStick = new Joystick(2); RobotDrive driveTrain = new RobotDrive(1,2); //Autonomous Section public void autonomous() { driveTrain.tankDrive(0.5, 0.5); Timer.delay(1); driveTrain.tankDrive(0, 0); } //Teleop Section public void operatorControl() { while (isOperatorControl() && isEnabled()) { driveTrain.tankDrive(leftStick.getY(), rightStick.getY()); } } } Resources http://first.wpi.edu/Images/CMS/First/Getting_Started_with_Java_for_FRC.pdf Category:FRC-Java